﻿using NUnit.Framework;
using Shouldly;
using System.Collections.Generic;
using DotNetCommon.Extensions;
using System.Text.Json.Nodes;

namespace DotNetCommon.Test.Extensions
{
    [TestFixture]
    public class ObjectTests_IfNullUse
    {
        [Test]
        public void Test()
        {
            int? num = null;
            num.IfNullOrInitValueUse(5).ShouldBe(5);
            num.IfNullUse(0).ShouldBe(0);
            num = 0;
            num.IfNullOrInitValueUse(5).ShouldBe(5);

            var num2 = 0;
            num2.IfDefaultUse(2).ShouldBe(2);

            bool? flag = null;
            flag.IfNullOrInitValueUse(false).ShouldBe(false);
            flag.IfNullUse(true).ShouldBe(true);
            flag = false;
            flag.IfNullOrInitValueUse(true).ShouldBe(true);

            byte? b = null;
            b.IfNullOrInitValueUse((byte)5).ShouldBe((byte)5);
            b.IfNullUse((byte)5).ShouldBe((byte)5);
            b = (byte)0;
            b.IfNullOrInitValueUse((byte)5).ShouldBe((byte)5);


            object obj = null;
            obj.IfNullUse(new object()).ShouldNotBeNull();
            obj.IfNullUseNew().ShouldNotBeNull();
        }
        [Test]
        public void Test2()
        {
            //集合null
            List<int> nums = null;
            nums.IfNullUseEmpty().Count.ShouldBe(0);

            //数组null
            int[] ints = null;
            ints.IfNullUseEmpty().Length.ShouldBe(0);
            ints.IfNullUseEmpty().Length.ShouldBe(0);

            //字典null
            Dictionary<string, string> dic = null;
            dic.IfNullUseEmpty().Count.ShouldBe(0);

            //字符串
            string str = null;
            str.IfNullOrEmptyUse("haha").ShouldBe("haha");
            str = "";
            str.IfNullOrEmptyUse("hehe").ShouldBe("hehe");
            str = "  ";
            str.IsNullOrWhiteSpaceUse("hehe").ShouldBe("hehe");
        }

        [Test]
        public void TestIfNullUseNew()
        {
            Person obj = null;
            var obj2 = obj.IfNullUseNew();
            obj2.ShouldNotBeNull();
        }

        [Test]
        public void TestIfNullUseNew_JsonObject_JsonArray()
        {
            JsonObject obj = null;
            var obj2 = obj.IfNullUseNew();
            obj2.ShouldNotBeNull();

            JsonArray arr = null;
            arr = arr.IfNullUseNew();
            obj2.ShouldNotBeNull();
        }

        class Person
        {
            public Person(string name, int num, int id = 1)
            {
                this.Name = name;
                this.Id = 1;
                this.Id += num;
            }
            public int Id { get; set; }
            public string Name { get; set; }
        }
    }
}
